﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace RexToy.Collections
{
    public class ReadOnlyList<T> : IReadOnlyList<T>
    {
        private List<T> _list;
        internal ReadOnlyList(List<T> list)
        {
            Assertion.IsNotNull(list, "list is null.");
            _list = list;
        }

        #region IReadOnlyList<T> Members

        public T this[int index]
        {
            get { return _list[index]; }
        }

        public int IndexOf(T item)
        {
            return _list.IndexOf(item);
        }

        public int Count
        {
            get { return _list.Count; }
        }

        public bool Contains(T item)
        {
            return _list.Contains(item);
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            _list.CopyTo(array, arrayIndex);
        }

        #endregion

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        #endregion

        public static implicit operator ReadOnlyList<T>(List<T> list)
        {
            return new ReadOnlyList<T>(list);
        }

        public static explicit operator List<T>(ReadOnlyList<T> list)
        {
            return list._list;
        }
    }
}
